package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.entity.*;
import com.yqg.manage.mapper.CaseMapper;
import com.yqg.manage.service.*;
import com.yqg.manage.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import org.springframework.web.bind.annotation.RestController;

/**
 * ArchiveBranchNodeController 前端控制器
 *
 * @author zxf
 * @since 2024-04-30
 */
@RestController
@RequestMapping("/archive-branch-node")
public class ArchiveBranchNodeController {

    @Resource
    private IArchiveBranchNodeService archiveBranchNodeService;

    @Autowired
    IParamsService paramsService;

    @Resource
    private IArchiveNodeService archiveNodeService;

    @Resource
    private IArchiveService archiveService;

    @Resource
    private ICaseService caseService;

    @Resource
    private CaseMapper caseMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody ArchiveBranchNode archiveBranchNode) {
        return Result.success(archiveBranchNodeService.saveOrUpdate(archiveBranchNode));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        ArchiveBranchNode archiveBranchNode = archiveBranchNodeService.getById(id);
        return Result.success(archiveBranchNodeService.saveOrUpdate(archiveBranchNode));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ArchiveBranchNode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<ArchiveBranchNode> list = archiveBranchNodeService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(archiveBranchNodeService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<ArchiveBranchNode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(archiveBranchNodeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    //查找下一节点分支
    @GetMapping("/findNextNode")
    public Result findNextNode(@RequestParam Integer routineTypeId, @RequestParam Integer archiveNodeId) {
        List<ArchiveBranchNode> archiveBranchNodes = archiveBranchNodeService.findNextNode(routineTypeId, archiveNodeId);
        if (archiveBranchNodes == null || archiveBranchNodes.isEmpty()) {
            return Result.success();
        } else {
            return Result.success(archiveBranchNodes);
        }
    }

    //查找上一节点
    @GetMapping("/findLastNode")
    public Result findLastNode(@RequestParam Long caseId) {
        Case acase = caseMapper.getCaseById(caseId);
        List<Long> archiveBranchNodeIds = acase.getArchiveBranchNodeIds();
        Long archiveBranchNodeId;
        if (archiveBranchNodeIds.isEmpty()) {
//            已经是第一步了
            return Result.success();
        } else {
            archiveBranchNodeId = archiveBranchNodeIds.get(archiveBranchNodeIds.size() - 1);
        }
        ArchiveBranchNode archiveBranchNode = archiveBranchNodeService.getById(archiveBranchNodeId);
        LambdaQueryWrapper<ArchiveNode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArchiveNode::getNodeId, archiveBranchNode.getNodeId())
                .eq(ArchiveNode::getIsDelete,"0");
        List<ArchiveNode> archiveNodes = archiveNodeService.list(queryWrapper);

        if (acase.getRoutineTypeId() != archiveNodes.get(0).getRoutineTypeId()) {
            acase.setRoutineTypeId(archiveNodes.get(0).getRoutineTypeId());
        }
        acase.setArchiveNodeId(archiveNodes.get(0).getNodeId());
        archiveBranchNodeIds.remove(archiveBranchNodeIds.size() - 1);
        acase.setArchiveBranchNodeIds(archiveBranchNodeIds);
        caseService.saveOrUpdate(acase);

        List<Long> archiveIds = archiveNodes.stream().map(ArchiveNode::getArchiveId).collect(Collectors.toList());
        LambdaQueryWrapper<Archive> archiveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        archiveLambdaQueryWrapper.in(Archive::getId, archiveIds);
        Map<String, String> map = new HashMap<>();
        for (Archive archive : archiveService.list(archiveLambdaQueryWrapper)) {
            map.put(archive.getTemplateNo(), archive.getDocumentName());
        }
        return Result.success(map);
    }

}

